# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\Crypto\Random\OSRNG\posix.py
__revision__ = "$Id$"
__all__ = ["DevURandomRNG"]
import errno, os, stat
from .rng_base import BaseRNG
from Crypto.Util.py3compat import b


class DevURandomRNG(BaseRNG):
    def __init__(self, devname=None):
        if devname is None:
            self.name = "/dev/urandom"
        else:
            self.name = devname
        f = open(self.name, "rb", 0)
        fmode = os.fstat(f.fileno())[stat.ST_MODE]
        if not stat.S_ISCHR(fmode):
            f.close()
            raise TypeError("%r is not a character special device" % (self.name,))
        self._DevURandomRNG__file = f
        BaseRNG.__init__(self)

    def _close(self):
        self._DevURandomRNG__file.close()

    def _read(self, N):
        data = b("")
        while len(data) < N:
            try:
                d = self._DevURandomRNG__file.read(N - len(data))
            except IOError as e:
                if e.errno == errno.EINTR:
                    continue
                raise

            if d is None:
                return data
            if len(d) == 0:
                return data
            data += d

        return data


def new(*args, **kwargs):
    return DevURandomRNG(*args, **kwargs)
